"""
用户服务类
"""
from app.models.user import User
from app import db


class UserService:
    """用户服务类"""
    
    @staticmethod
    def get_user_stats():
        """获取用户统计数据"""
        try:
            # 总用户数
            total = User.query.count()
            
            # 按状态统计
            active_count = User.query.filter_by(status='在职').count()
            inactive_count = User.query.filter_by(status='离职').count()
            
            # 按角色统计
            admin_count = User.query.filter_by(role='admin').count()
            manager_count = User.query.filter_by(role='manager').count()
            employee_count = User.query.filter_by(role='employee').count()
            
            # 部门数量统计
            departments = db.session.query(User.department).distinct().filter(User.department.isnot(None)).count()
            
            return {
                'total': total,
                'active': active_count,
                'inactive': inactive_count,
                'admin': admin_count,
                'manager': manager_count,
                'employee': employee_count,
                'departments': departments
            }
        except Exception as e:
            # 返回默认统计数据，避免模板错误
            return {
                'total': 0,
                'active': 0,
                'inactive': 0,
                'admin': 0,
                'manager': 0,
                'employee': 0,
                'departments': 0
            }